﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LX.Core.Query;

namespace LX.Core.Model
{
    public abstract class QueryModel<T> : IQueryModel<T>
    {
        public int? Page { get; set; }
        public int? PageSize { get; set; }

        public string OrderField { get; set; }
        public OrderType OrderType { get; set; }

        public virtual EntityFilter<T> GetFilter()
        {
            EntityFilter<T> filter = new EntityFilter<T>();
            if (Page != null && Page > 0 && PageSize != null && PageSize > 0)
            {
                filter.PageTo(Page.Value, PageSize.Value);
            }
            if (!string.IsNullOrWhiteSpace(OrderField))
            {
                if (OrderType == Query.OrderType.Asc)
                {
                    filter.OrderBy(OrderField);
                }
                else
                {
                    filter.OrderByDescending(OrderField);
                }
            }
            Filter(filter);
            return filter;
        }

        public abstract void Filter(EntityFilter<T> filter);
    }
}
